package com.chis.jxc;

import com.alibaba.fastjson.JSON;
import com.chis.jm.bean.qczs.data.CntStmCode;
import com.chis.jmdatatimer.comm.TxMgmtModule;
import com.chis.jxc.bean.CkBean;
import com.chis.jxc.bean.CntCode;
import com.chis.jxc.bean.CodeBean;
import com.chis.jxc.bean.ElecInfo;
import com.chis.jxc.bean.FhBean;
import com.chis.jxc.bean.RkBean;
import com.chis.jxc.bean.TraceCode;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DealRkCode {

    public static void main(String[] args) {
        try {
            String datesta = args[0];
            String dateend = args[1];
            String ifdeal = args[2];
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring.xml"});
            context.start();
            TxMgmtModule txMgmtModule = (TxMgmtModule) context.getBean("TxMgmtModule");
            List<RkBean> list = txMgmtModule.findRkInfo(new SimpleDateFormat("yyyy-MM-dd").parse(datesta),new SimpleDateFormat("yyyy-MM-dd").parse(dateend));
            if(list != null && list.size() > 0){
                List<String> ruguids = new ArrayList<>();
                Map<String,RkBean> ruinfomap = new HashMap<>();

                List<String> ckcodes = new ArrayList<>();
                List<String> fhcodes = new ArrayList<>();
                for(RkBean bean : list){
                    ruguids.add(bean.getRuguid());
                    ruinfomap.put(bean.getRuckcode()+"#"+bean.getBactcode()+"#"+bean.getBatnmb()+"#"+bean.getBatnum(),bean);
                    if(bean.getRuckcode().startsWith("CK")){
                        if(!ckcodes.contains(bean.getRuckcode())){
                            ckcodes.add(bean.getRuckcode());
                        }
                    } else if(bean.getRuckcode().startsWith("FH")){
                        if(!fhcodes.contains(bean.getRuckcode())){
                            fhcodes.add(bean.getRuckcode());
                        }
                    }

                }
                System.out.println("入库记录："+ruguids.size());
                List<CkBean> cklist = new ArrayList<>();
                List<CkBean> fhlist = new ArrayList<>();
                Map<String,CkBean> ckmap = new HashMap<>();
                Map<String,CkBean> fhmap = new HashMap<>();
                List<String> ckguids = new ArrayList<>();
                List<String> fhguids = new ArrayList<>();

                if(ckcodes.size() > 0){
                    StringBuilder sb = new StringBuilder();
                    for(String s : ckcodes){
                        sb.append(",'").append(s).append("'");
                    }
                    cklist = txMgmtModule.findckguid(sb.substring(1));
                    for(CkBean bean : cklist){
                        ckguids.add(bean.getCkguid());
                        ckmap.put(bean.getCkguid(),bean);
                    }

                }
                System.out.println("出库记录："+cklist.size());
                if(fhcodes.size() > 0){
                    StringBuilder sb = new StringBuilder();
                    for(String s : fhcodes){
                        sb.append(",'").append(s).append("'");
                    }
                    fhlist = txMgmtModule.findfhguid(sb.substring(1));
                    for(CkBean bean : fhlist){
                        fhguids.add(bean.getCkguid());
                        fhmap.put(bean.getCkguid(),bean);
                    }
                }
                System.out.println("发货记录："+fhlist.size());

                List<CodeBean> rkcode = txMgmtModule.findEleccode(1,ruguids);
                List<CodeBean> ckcode = txMgmtModule.findEleccode(2,ckguids);
                List<CodeBean> fhcode = txMgmtModule.findEleccode(3,fhguids);

                List<String> l1 = new ArrayList<>();
                List<String> l2 = new ArrayList<>();
                Map<String,CodeBean> ckcodemap = new HashMap<>();
                Map<String,CodeBean> fhcodemap = new HashMap<>();
                for(CodeBean bean : ckcode){
                    l1.add(bean.getElecCode());
                    ckcodemap.put(bean.getElecCode(),bean);
                }
                for(CodeBean bean : fhcode){
                    l1.add(bean.getElecCode());
                    fhcodemap.put(bean.getElecCode(),bean);
                }
                for(CodeBean  bean : rkcode){
                    l2.add(bean.getElecCode());
                }
                System.out.println("丢失监管码：");
                for(String s : l1){
                    if(!l2.contains(s)){
                        System.out.println(s);
                        if(ifdeal.equals("1") || ifdeal.equals("2")){
                            //入库码表
                            CodeBean codeBean = null;
                            CkBean ckbean = null;
                            if(ckcodemap.containsKey(s)){
                                codeBean = ckcodemap.get(s);
                                ckbean = ckmap.get(codeBean.getGuid());
                            } else{
                                codeBean = fhcodemap.get(s);
                                ckbean = fhmap.get(codeBean.getGuid());
                            }
                            RkBean rkBean = ruinfomap.get(ckbean.getCkcode()+"#"+ckbean.getBactcode()+"#"+ckbean.getBatnmb()+"#"+ckbean.getBatnum());
                            codeBean.setGuid(rkBean.getRuguid());
                            if(ifdeal.equals("2")){
                                txMgmtModule.savePursubCode(codeBean);
                            } else{
                                System.out.println("入库码表:");
                                String str = JSON.toJSONString(codeBean);
                                System.out.println(str);
                            }

                            //码库存
                            List<CntCode> listcnt = new ArrayList<>();
                            if(codeBean.getCodeType() == 1){

                                CntCode cntCode = new CntCode();
                                cntCode.setElecCode(codeBean.getElecCode());
                                cntCode.setCodetype(codeBean.getCodeType());
                                cntCode.setMiddleCode(codeBean.getMiddlecode());
                                cntCode.setBigCode(codeBean.getBigcode());
                                cntCode.setUptime(System.currentTimeMillis());
                                cntCode.setCntguid(rkBean.getCntguid());
                                cntCode.setBalance(rkBean.getBalance());
                                listcnt.add(cntCode);
                            }else if (codeBean.getCodeType() == 2){
                                CntCode cntCode = new CntCode();
                                cntCode.setElecCode(codeBean.getElecCode());
                                cntCode.setCodetype(codeBean.getCodeType());
                                cntCode.setBigCode(codeBean.getBigcode());
                                cntCode.setUptime(System.currentTimeMillis());
                                cntCode.setCntguid(rkBean.getCntguid());
                                cntCode.setBalance(null);
                                listcnt.add(cntCode);
                                List<ElecInfo> elecInfo = txMgmtModule.findElecInfo(2,codeBean.getElecCode());
                                for(ElecInfo e : elecInfo){
                                    cntCode = new CntCode();
                                    cntCode.setElecCode(e.getElecCode());
                                    cntCode.setCodetype(e.getCodeType());
                                    cntCode.setMiddleCode(e.getMiddlecode());
                                    cntCode.setBigCode(e.getBigcode());
                                    cntCode.setBalance(rkBean.getBalance());
                                    cntCode.setUptime(System.currentTimeMillis());
                                    cntCode.setCntguid(rkBean.getCntguid());
                                    listcnt.add(cntCode);
                                }
                            }else if (codeBean.getCodeType() == 3){
                                CntCode cntCode = new CntCode();
                                cntCode.setElecCode(codeBean.getElecCode());
                                cntCode.setCodetype(codeBean.getCodeType());
                                cntCode.setUptime(System.currentTimeMillis());
                                cntCode.setCntguid(rkBean.getCntguid());
                                cntCode.setBalance(null);
                                listcnt.add(cntCode);
                                List<ElecInfo> elecInfo = txMgmtModule.findElecInfo(3,codeBean.getElecCode());
                                for(ElecInfo e : elecInfo){
                                    cntCode = new CntCode();
                                    cntCode.setElecCode(e.getElecCode());
                                    cntCode.setCodetype(e.getCodeType());
                                    cntCode.setBigCode(e.getBigcode());
                                    cntCode.setUptime(System.currentTimeMillis());
                                    cntCode.setCntguid(rkBean.getCntguid());
                                    cntCode.setBalance(null);
                                    listcnt.add(cntCode);
                                    List<ElecInfo> elecInfo2 = txMgmtModule.findElecInfo(2,e.getElecCode());
                                    for(ElecInfo e3 : elecInfo2){
                                        cntCode = new CntCode();
                                        cntCode.setElecCode(e3.getElecCode());
                                        cntCode.setCodetype(e3.getCodeType());
                                        cntCode.setMiddleCode(e3.getMiddlecode());
                                        cntCode.setBigCode(e3.getBigcode());
                                        cntCode.setBalance(rkBean.getBalance());
                                        cntCode.setUptime(System.currentTimeMillis());
                                        cntCode.setCntguid(rkBean.getCntguid());
                                        listcnt.add(cntCode);
                                    }
                                }
                            }
                            if(ifdeal.equals("2")){
                                for(CntCode t : listcnt){
                                    txMgmtModule.saveCntCode(t);
                                }
                            } else{
                                System.out.println("库存码表:");
                                String str = JSON.toJSONString(listcnt);
                                System.out.println(str);
                            }

                            //更新追溯码表
                            TraceCode traceCode= txMgmtModule.findtracecode(s);
                            traceCode.setPurCode(rkBean.getCntguid());
                            if(ifdeal.equals("2")){
                                txMgmtModule.updateCntCode(traceCode);
                            } else{
                                System.out.println("追溯码表:");
                                String str = JSON.toJSONString(traceCode);
                                System.out.println(str);
                            }

                        }
                    }
                }


            } else{
                System.out.println("没有查询到入库单");
            }

        } catch (Exception e){
            e.printStackTrace();
        }


    }
}
